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kuaWYRkQnHmsV4
YjPetcvs9T6AUGrnDLZyIpacD9Zn5f0mXm943Yc/3aSIAJC1WgUnOc3tChW3Zkig
ApyZdp24AQKBgBqILOrSjpYwMC6B2SWqMMK1mFQXyELmHsBAZcOEdjPXmIXN3ChU
nDZSJIcXik3et3rnJ1PHB9PtZdXnS7o0G2FL10Vg/9uFRrLvMSkz5cSdk+ngfEBM
f9IKC3YutotzWE7GCBVWk8BoqC3+Eiol4ToRhYcL3El3la56CtWidajZAoGBAIgr
AcBsjArxYJLm1EEwQFh0ZZFSmxIUukFT+NSyqORmSou6FDNp82YHan1iYyad41dp
OHEu2+J/dHvmpV9pUjY+5JMAP1t76WrS+9l+03i6Gcnw+3hviVmqoh8C15azQKlF
UJIel51yhWmgEdVC2LXcCcd4HfLeDt5CzUTAfKgBAoGBAMAhKyPXm7MmZEzqncBI
oIVEwNVu6woHlMrEQtvA5tgoYxf7dOBluTgAPnYEHJfllDP2H0OlBQWD6xBuhvsq
7G6wHf2d0p4ldfeNsl9nTYvwlN3ChDu6/W+UBB769T3vLJJiU217Kzq6/PWkrKe0
a0YR/ROI/uB3FHcOlNJwyxiw
-----END PRIVATE KEY-----
`;

exports.sha256withRSA = function sha256withRSA(inputString) {
  const key = jsign.KEYUTIL.getKey(privateKeyString);
  // 创建 Signature 对象，设置签名编码算法
  const signature = new jsign.KJUR.crypto.Signature({ alg: "SHA256withRSA" });
  // 初始化
  signature.init(key);
  // 传入待加密字符串
  signature.updateString(inputString);
  // 生成密文
  const originSign = signature.sign();
  const sign64 = jsign.hextob64(originSign);
  // console.log('sign base64 =======', sign64);
  // const sign64u = hextob64u(originSign);
  // console.log('sign base64u=======', sign64u);
  console.log(inputString);
  return sign64;
};
